#include "head.h"
/* Usage: */

client_status* log_or_re(int net_fd, MYSQL *sql){
    c_sends msg;
    int ret = recv(net_fd, &msg, sizeof(msg), MSG_WAITALL);
    
    if(ret==0){
        printf("用户已断开！\n");
        return NULL;
    }
    if(msg.isstatus==1){
        return login(net_fd, msg, sql);
    } else if(msg.isstatus==0){
        return regist(net_fd, msg, sql);
    }
}

client_status* login(int net_fd, c_sends msg, MYSQL *sql){
    recvMsg recv_msg;
    bool token_flag = false;
    printf("%s\n", msg.token);
    if(strcmp(msg.token, "")){
        detoken(msg.token, &msg);
        token_flag = true;
        printf("id = %s\n", msg.user_id);
    }
    
    char name[1000];
    client_status *user = (client_status*)calloc(1, sizeof(client_status));
    user->sql = sql;
    user->net_fd = net_fd;
    //recv(net_fd,&msg,sizeof(msg),0);

    char user_id[10]={0};
    char password[1000]={0};

   // user->user_name[strlen(user->user_name)-1] = 0;
   // strcpy(name, msg.user_name);
    strcpy(password,msg.password);
    strcpy(user_id,msg.user_id);

   // printf("user_id = %s\n",user_id);0

    char query[1000]={0};
    snprintf(query, sizeof(query), "SELECT * FROM user WHERE uid='%s'",user_id);
    //对比用户id和密码和数据库的内容是否一直
    mysql_query(sql,query);
    MYSQL_RES *result=mysql_store_result(sql);
    if(!result) printf("???\n");
    printf("num = %ld\n", mysql_num_rows(result));
    if(mysql_num_rows(result)==0){
        strcpy(recv_msg.statu, "-1");
        strcpy(recv_msg.msg, "用户不存在！\n");
        return NULL;
    }
    printf("用户存在！\n");
    //从结果中取出一行
    MYSQL_ROW row=mysql_fetch_row(result);



    // char salt[1000];
    // strcpy(salt,row[salt_value]);

    char *salt=row[salt_value];
    char *hash=row[psw_hash];

    //计算hash值
    char *hash_password=crypt(password,salt);
    //给客户端发送重新输入账号密码
    if(strcmp(hash,hash_password)!=0){
        strcpy(recv_msg.statu, "-1");
        strcpy(recv_msg.msg, "登陆失败！");
        send(net_fd, &recv_msg, sizeof(recv_msg), 0);
        return NULL;
    }
    strcpy(name,row[user_name]);
    strcpy(recv_msg.statu, "1");
    //strcpy(recv_msg.msg, "登陆成功！");
    strcpy(user->user_id, user_id);
   // strcpy(user->user_id, user_id);
    strcpy(user->user_name,name);
    strcpy(recv_msg.msg,name);
    
    memset(query,0,sizeof(query));

    snprintf(query, sizeof(query), "update user set islogin='1' WHERE uid='%s'",user_id);
    mysql_query(sql,query);
    char token[2000] = {0};
    if(!token_flag){
        entoken(msg, token);
        printf("token = %s\n", token);
        strcpy(recv_msg.token, token);
    } else {
        strcpy(recv_msg.token, "");
    }
    strcpy(recv_msg.statu, "1");
    send(net_fd, &recv_msg, sizeof(recv_msg), MSG_NOSIGNAL);
    //密码输对了,继续往后面输入
    strcpy(user->path, "");
    user->islogin = true;
    return user;
}

int getsalt(char *dest){
    srand(time(0));
    dest[0] = dest[7] = dest[2] = '$';
    dest[1] = '6';
    for(int i=3; i<7; i++){
        //dest[i] = 32 + rand()%94;
        dest[i] = 46 + rand()%11;
    }
    return 1;
}

client_status* regist(int net_fd, c_sends msg, MYSQL *sql){
    MYSQL_RES *result;
    MYSQL_ROW row;
    recvMsg recv_msg;
    
    char cmd[4096] = {0};
    snprintf(cmd, sizeof(cmd), "select * from user where uid='%s'", msg.user_id);
    mysql_query(sql, cmd);
    result = mysql_store_result(sql);    
    printf("%ld\n", mysql_num_rows(result));
    if(mysql_num_rows(result)!=0){
        strcpy(recv_msg.statu, "-1");
        printf("账号重复！\n");
        strcpy(recv_msg.msg, "账号重复！");
        send(net_fd, &recv_msg, sizeof(recv_msg), 0);
        return NULL;
    }

    char salt[20] = {0};
    getsalt(salt);
    strcpy(msg.password, crypt(msg.password, salt));
    snprintf(cmd, sizeof(cmd), "insert into user value ('%s', '%s', '%s', '%s', '%s', '0', now(), '0')", msg.user_id, msg.user_name, msg.user_id, msg.password, salt);
    mysql_query(sql, cmd);

    snprintf(cmd, sizeof(cmd), "insert into files set file_name='/', uid='%s', pre_dir=-1, path='/', create_time=now(), file_type='1', finish='1'", msg.user_id);
    mysql_query(sql, cmd);
    

    snprintf(cmd, sizeof(cmd), "update user set dir_id=(select id from files where uid=user.uid limit 1) where uid='%s'", msg.user_id);
    mysql_query(sql, cmd);

    strcpy(recv_msg.statu, "1");
    strcpy(recv_msg.msg, "注册成功！");
    printf("注册成功！\n");
    send(net_fd, &recv_msg, sizeof(recv_msg), 0);

    return NULL;
}

bool unlogin(client_status *user, int net_fd){
    printf("%s 退出登录！\n", user->user_id);
    char cmd[1000] = {0};
    snprintf(cmd, 1000, "update user set islogin=\"0\" where uid=%s", user->user_id);
    mysql_query(user->sql, cmd);
    free(user);
    recvMsg msg;
    strcpy(msg.statu, "1");
    strcpy(msg.msg, "退出成功！");
    send(net_fd, &msg, sizeof(msg), 0);
    close(net_fd);
    return true;
}
